<template>
  <div>
      <h2>管理员----增加投资项目</h2>
      <table class="table table-borderd">
          <tr>
            <td align="right">投资名称：</td>
            <td><input type="text" v-model="state.I_Name"/></td>
          </tr>
          <tr>
            <td align="right">投资总金额:</td>
            <td><input type="text" v-model="state.I_Budget"/></td>
          </tr>
          <tr>
            <td></td>
            <td><input type="button" value="增加投资" @click="add"/></td>
          </tr>
      </table>
  </div>
</template>

<script setup lang="ts">
import {reactive,toRefs} from 'vue'
import axios from 'axios';
import { useRouter } from 'vue-router'

const router = useRouter();
let data = reactive({
  state:{
    I_Name:'',
    I_Budget:''
  }
})

let {state } = toRefs(data)

//增加投资
const add = ()=>{
  //1.非空+有效性验证
  if(state.value.I_Name == ""){
    alert("投资名称不能为空");
    return;
  }

  if(state.value.I_Budget == ""){
    alert("投资总金额不能为空");
    return;
  }
  if(isNaN(Number(state.value.I_Budget)) == true){
    alert("投资总金额必须是数字");
    return
  }
  if(Number(state.value.I_Budget) < 100000 || Number(state.value.I_Budget)>1000000){
    alert("投资总金额在100000-1000000之间")
    return;
  }
  //2.发送网络请求实现添加效果
  axios({
    url:'/api/Invest/PostAddInvest',
    method:'post',
    data:state.value
  })
  .then(res=>{
    console.log(res);
    if(res.data.data>0){
      alert("增加投资成功");
      //跳转列表
      router.push('/main/adminInvestList')
    }else if(res.data.data == -1){
      alert("投资名称重复，请换一个名字");
      return;
    }else{
      alert("添加失败");
      return;
    }
    
  })
  .catch(err=>{
    console.log(err);
    
  })
}
</script>

<style scoped>

</style>